package com.mjc.book.controller;

import com.mjc.book.model.Book;
import com.mjc.book.service.BookService;
import com.mjc.book.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 接口层，供前端调用
 * @author admin
 */
@Api(tags = "图书管理" )
@RestController
@RequestMapping(value = "/book")
public class BookController {

    @Autowired
    BookService bookService;

    /**
     * 获取所有的书
     * @return
     */
    @ApiOperation(value = "获取所有的书", notes = "")
    @GetMapping(value = "/list")
    public Result bookList(){
        List<Book> all = bookService.findAll();
        return new Result().success(200, all);
    }

    /**
     * 添加图书
     * @param book
     * @return
     */
    @ApiOperation(value = "添加图书", notes = "")
    @PostMapping(value = "/save")
    public Result bookSave(@RequestBody  Book book){
        bookService.creatBook(book);
        return new Result().success(200, "成功");
    }

    /**
     * 更新图书
     * @param book
     * @return
     */
    @ApiOperation(value = "更新图书", notes = "")
    @PostMapping(value = "/update")
    public Result bookUpdate(@RequestBody Book book){
        bookService.updateBook(book);
        return new Result().success(200, "成功");
    }

    /**
     * 删除图书
     * @param id
     * @return
     */
    @ApiOperation(value = "删除图书", notes = "")
    @PostMapping(value = "/delete/{id}")
    public Result bookDelete(@PathVariable(value = "id") int id){
        bookService.deleteBook(id);
        return new Result().success(200, "成功");
    }
    
}
